{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#coding:utf-8\n",
    "import os\n",
    "import jieba\n",
    "\n",
    "\n",
    "# 创建停用词list\n",
    "def stopwordslist(filepath):\n",
    "    stopwords = [line.strip() for line in open(filepath, 'r', encoding='utf-8').readlines()]\n",
    "    return stopwords\n",
    "\n",
    "# 对句子进行分词\n",
    "def seg_sentence(sentence, cut_engine, stopwords):\n",
    "    sentence_seged = cut_engine.cut(sentence.strip())\n",
    "    seg_list = []\n",
    "    for word in sentence_seged:\n",
    "        if word not in stopwords:\n",
    "            seg_list.append(word)\n",
    "    return seg_list\n",
    "\n",
    "def cutFile(path, file, cut_engine, stopwords, all_title_list):\n",
    "    filePath = path+file\n",
    "    with open(filePath,\"rb\") as fin:\n",
    "        line = fin.readline().decode('utf-8').lower()\n",
    "        while(line):\n",
    "            sentence = line.split(sep=\"\\u0001\")[1].strip()\n",
    "            all_title_list.append(seg_sentence(sentence, cut_engine, stopwords))\n",
    "            line = fin.readline().decode('utf-8').lower()\n",
    "\n",
    "def cutFiles(path, cut_engine, stopwords, all_title_list):\n",
    "    fileNames = os.listdir(path)\n",
    "    for file in fileNames:\n",
    "        cutFile(path,file,cut_engine,stopwords,all_title_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "end\n"
     ]
    }
   ],
   "source": [
    "path = \"./job_title/\"\n",
    "zhiliandict = \"./my_dict/zhilian.dict\"\n",
    "jieba.load_userdict(zhiliandict)\n",
    "stopwords = stopwordslist('./stopwords/stopwords.txt')  # 这里加载停用词的路径\n",
    "all_title_list = []\n",
    "cutFiles(path, jieba, stopwords, all_title_list)\n",
    "with open(\"./titles_seg/titles_seg.txt\", \"w\", encoding=\"utf-8\") as fout:\n",
    "    for title_list in all_title_list:\n",
    "        line = \"\"\n",
    "        for word in title_list:\n",
    "            line += \" \" + word\n",
    "        fout.write(line.strip())\n",
    "        fout.write(\"\\n\")\n",
    "\n",
    "print(\"end\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [default]",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
